<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="1"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />

    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import {setSecurityCode,updateSecurityCode,selectByPJSX} from '@/api/customerService/SecurityCode/obtainMode/index';
  export default {
    name: "addObtainMode",
    mixins: [dictionary],
    props: {
      type: {
        type: String,
        default: 'add',
      },
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData = null;
      return {
        formList: this.createFormList(),
        fetchApi: null,
        saveLoading: false,
        bsckid: '',
        fetchParams: null,
      };
    },
    created() {
      this.init();
    },
    methods: {
      init() {
        console.info(this.type)
        console.info(this.rowData)
        if (this.type == 'edit'){
          this.formList.find(item=>item.fieldName=="attribute").disabled=true
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
        }
      },
      createFormList() {
        return [
          {
            type: 'SELECT',
            label: '油电属性',
            fieldName: 'attribute',
            itemList:this.createDictList('5000'),
            initialValue: '',
            rules: [{ required: true, message: '请选择油电属性', trigger: 'blur' }]
          },
          {
            type: 'SELECT',
            label: '获取方式',
            fieldName: 'acquisitionMethod',
            itemList:this.createDictList('5013'),
            initialValue: '',
            rules: [{ required: true, message: '请选择获取方式', trigger: 'blur' }]
          },
        ]
      },
      formChangeHandle(val) {
        this.saveData = val;
      },
      async saveDialog() {
        console.info(this.saveData)
        console.info(this.rowData)
        const err = this.$refs.formPanel.SUBMIT_FORM();
        if (err === null) {
          return false
        }
        this.saveLoading = true
        console.info(this.saveData)
          if (this.type === 'edit') {
            this.saveData.id = this.rowData.id
            const res = await updateSecurityCode(this.saveData);
            if (res.resultCode === 200 ) {
              this.closeDialog('refresh');
            }
          }else{
            const result = await selectByPJSX(this.saveData)
            console.info(result)
            if (result.data.length == 0){
              const res = await setSecurityCode(this.saveData);
              if (res.resultCode === 200 ) {
                this.closeDialog('refresh');
              }
            } else {
              this.$notify.error({title: '提示信息', message: '该属性已存在VIN码获取方式'});
            }
          }
        this.saveLoading = false
      },
      seacherFun(type){
        if (type == 1) {
          this.issearch = true
        } else {
          this.selectCentral = true
        }

      },
      closeDialog(param) {
        this.$emit('close', {visible: false, type: param});
      }
    }
  }
</script>

<style scoped>

</style>
